package mapEditor.view.panels
{
	import asset.mapEditor.panelBaseAsset;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	import mapEditor.common.DisplayObjectManager;
	
	public class PanelBaseView extends panelBaseAsset
	{
		private var _title:String;
//		private var _description:String;
//		private var _previewObject:DisplayObject;
		private var _itemList:Vector.<PanelItemBaseView>=new Vector.<PanelItemBaseView>();
		
		public function PanelBaseView(title:String="")
		{
			_title=title;
			txtTitle.text=_title;
			
			if(listPos.parent) listPos.parent.removeChild(listPos);
			
			setEvent();
			
			btnClose.buttonMode=btnAlpha.buttonMode=true;
			txtTitle.mouseEnabled=false;
		}
		
		private function setEvent():void
		{
			boxDrag.addEventListener(MouseEvent.MOUSE_DOWN, __onMouseDown);
			boxDrag.addEventListener(MouseEvent.MOUSE_OVER, __onMouseOver);
			boxDrag.addEventListener(MouseEvent.MOUSE_UP, __onMouseUp);
			btnClose.addEventListener(MouseEvent.CLICK, __onCloseClick);
			btnAlpha.addEventListener(MouseEvent.CLICK, __onAlphaClick);
		}
		
		private function removeEvent():void
		{
			boxDrag.removeEventListener(MouseEvent.MOUSE_DOWN, __onMouseDown);
			boxDrag.removeEventListener(MouseEvent.MOUSE_OVER, __onMouseOver);
			boxDrag.removeEventListener(MouseEvent.MOUSE_UP, __onMouseUp);
			btnClose.removeEventListener(MouseEvent.CLICK, __onCloseClick);
			btnAlpha.removeEventListener(MouseEvent.CLICK, __onAlphaClick);
		}
		
		private function __onAlphaClick(evt:MouseEvent):void
		{
			if(alpha<=0.5)
			{
				alpha=1;
			}
			else
			{
				alpha=0.5;
			}
			
		}
		
		private function __onMouseOver(evt:MouseEvent):void
		{
			Mouse.cursor=MouseCursor.AUTO;
		}		
		
		private function __onMouseDown(evt:MouseEvent):void
		{
			startDrag();
		}
		
		private function __onMouseUp(evt:MouseEvent):void
		{
			stopDrag();
		}
		
		/**
		 *增加项 
		 */
		public function addItem(item:PanelItemBaseView):void
		{
			item.x=listPos.x;
			item.y=itemTotalHeight+listPos.y;
			addChild(item);
			_itemList.push(item);
			
			updateHeight();
		}
		
		/**
		 *取得所有项高度总和 
		 */		
		public function get itemTotalHeight():Number
		{
			var totalHeight:Number=0;
			for each(var item:PanelItemBaseView in _itemList)
			{
				if(item)
				{
					totalHeight+=item.height;
				}
			}
			return totalHeight;
		}
		
		/**
		 *更新高度同时刷新背景高度
		 */		
		public function updateHeight():void
		{
			var totalHeight:Number=0;
			for each(var item:PanelItemBaseView in _itemList)
			{
				if(item)
				{
					totalHeight+=item.height;
				}
			}
			
			panelBg.height=totalHeight+10;
		}
		
		/**
		 *关闭面板 
		 */
		private function __onCloseClick(evt:MouseEvent):void
		{
			dispatchEvent(new Event(Event.CLOSE));
		}
		
		/**
		 *隐藏面板 
		 */		
		private function hideView():void
		{
			visible=false;
		}
		
		/**
		 *面板标题 
		 */		
		public function get title():String
		{
			return _title;
		}
		
		/**
		 *面板标题 
		 */
		public function set title(value:String):void
		{
			_title = value;
			txtTitle.text=_title;
		}
		
//		/**
//		 *面板描述 
//		 */		
//		public function get description():String
//		{
//			return _description;
//		}
//		
//		/**
//		 *面板描述 
//		 */	
//		public function set description(value:String):void
//		{
//			_description = value;
//			txtDescription.text=_description;
//		}
		
//		/**
//		 *预览显示对象 
//		 */		
//		public function get previewObject():DisplayObject
//		{
//			return _previewObject;
//		}
//		
//		/**
//		 *预览显示对象 
//		 */
//		public function set previewObject(value:DisplayObject):void
//		{
//			if(_previewObject && _previewObject.parent) _previewObject.parent.removeChild(_previewObject);
//			_previewObject=null;
//			
//			_previewObject = value;
//			if(!_previewObject) return;
//			
//			boxPreview.txtDefault.visible=false;
//			DisplayObjectManager.thumbnailShow(_previewObject, 38, 38);
//			boxPreview.addChild(_previewObject);
//		}
		
		public function dispose():void
		{
			removeEvent();
			
//			if(_previewObject && _previewObject.parent) _previewObject.parent.removeChild(_previewObject);
//			_previewObject=null;
			
			for each(var item:PanelItemBaseView in _itemList)
			{
				if(item)
				{
					if(item.parent) item.parent.removeChild(item);
					item.dispose();
				}
				item=null;
			}
			_itemList=null;
			
			if(parent) parent.removeChild(this);
		}

		public function get itemList():Vector.<PanelItemBaseView>
		{
			return _itemList;
		}

		public function set itemList(value:Vector.<PanelItemBaseView>):void
		{
			_itemList = value;
		}

	}
}